/*
 * @brief Flash signature example using a flash signature generator
 *
 * @note
 * Copyright(C) NXP Semiconductors, 2013
 * All rights reserved.
 *
 * @par
 * Software that is described herein is for illustrative purposes only
 * which provides customers with programming information regarding the
 * LPC products.  This software is supplied "AS IS" without any warranties of
 * any kind, and NXP Semiconductors and its licensor disclaim any and
 * all warranties, express or implied, including all implied warranties of
 * merchantability, fitness for a particular purpose and non-infringement of
 * intellectual property rights.  NXP Semiconductors assumes no responsibility
 * or liability for the use of the software, conveys no license or rights under any
 * patent, copyright, mask work right, or any other intellectual property rights in
 * or to any products. NXP Semiconductors reserves the right to make changes
 * in the software without notification. NXP Semiconductors also makes no
 * representation or warranty that such application will be suitable for the
 * specified use without further testing or modification.
 *
 * @par
 * Permission to use, copy, modify, and distribute this software and its
 * documentation is hereby granted, under NXP Semiconductors' and its
 * licensor's relevant copyrights in the software, without fee, provided that it
 * is used in conjunction with NXP Semiconductors microcontrollers.  This
 * copyright, permission, and disclaimer notice must appear in all copies of
 * this code.
 */

#include "board.h"
#include <stdio.h>

/*****************************************************************************
 * Private types/enumerations/variables
 ****************************************************************************/

#define DATA1 0x13234312
#define DATA2 0xAF78E902
#define DATA3 0x915ADAF2
#define DATA4 0xCB175B6A
#define DATA5 0xE3765B32
#define DATA6 0x6E6F9A0A
#define DATA7 0x518FB8CB
#define DATA8 0x998877AA

/* Signature generator test data */
#if defined(__CC_ARM)
/* Keil alignement to 16 bytes */
__align(16) static const uint32_t array_data[8] =
{DATA1, DATA2, DATA3, DATA4, DATA5, DATA6, DATA7, DATA8};
#endif /* defined (__CC_ARM) */

/* IAR support */
#if defined(__ICCARM__)
/* IAR EWARM alignement to 16 bytes */
#pragma data_alignment=16
static const uint32_t array_data[8] =
{DATA1, DATA2, DATA3, DATA4, DATA5, DATA6, DATA7, DATA8};
#endif /* defined (__ICCARM__) */

#if defined( __GNUC__ )
/* GNU alignement to 16 bytes */
static const uint32_t array_data[8] __attribute__ ((aligned(16))) =
{DATA1, DATA2, DATA3, DATA4, DATA5, DATA6, DATA7, DATA8};
#endif /* defined (__GNUC__) */

/* Signature start address */
#define START_ADDR                  0x00

/* Signature end address for the first 1K bytes */
#define END_ADDR                    0x400

/* Expected signature that is generated for array_data */
#define EXPECTED_8_WORDS_SIGNATURE  0xEAE7FABB

/*****************************************************************************
 * Private functions
 ****************************************************************************/

/**
 * @brief	Main program body
 * @return	Does not return
 */
int main(void)
{
	/* Start the signature generator for 8 workds data array */
	Chip_FMC_ComputeSignature((uint32_t) &array_data[0], (uint32_t) &array_data[7]);

	/* Check for signature generation completion */
	while (Chip_FMC_IsSignatureBusy()) {}

	/* Show the expected FLASH signature value */
	DEBUGOUT("Expected 8 words signature is: %x \r\n", EXPECTED_8_WORDS_SIGNATURE);

	/* Get the generated FLASH signature value */
	DEBUGOUT("Generated 8 words signature is: %x \r\n", Chip_FMC_GetSignature(0));

	/* Start the signature generator for the first 1K bytes */
	Chip_FMC_ComputeSignature(START_ADDR, END_ADDR);

	/* Check for signature geenration completion */
	while (Chip_FMC_IsSignatureBusy()) {}

	/* Get the generated FLASH signature value */
	DEBUGOUT("Generated 1K bytes signature is: %x \r\n", Chip_FMC_GetSignature(0));

	return 0;
}
